//
//  CommonXmlParser.m
//  MyShoes
//
//  Created by mac on 11-4-8.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CommonXmlParser.h"
#import "DDXMLDocument.h"
#import "DDXMLElement.h"
#import "Common.h"


@implementation CommonXmlParser

+ (NSMutableDictionary *)parseSimpleDDXMLElement:(DDXMLElement *)element{
	NSMutableDictionary *xitem = [[[NSMutableDictionary alloc]init]autorelease];
	for (DDXMLElement *e in [element children]) {
        [xitem setObject:e.stringValue forKey:[e name]];
	}
	return xitem;
}

+ (NSMutableArray *)parseSimpleDDXMLElementArray:(NSArray *)items{
	NSMutableArray *xitems = [[[NSMutableArray alloc]init]autorelease];
	for (DDXMLElement *item in items) {
		NSMutableDictionary *xitem = [self parseSimpleDDXMLElement:item];
		for (DDXMLNode *e in [item attributes]) {
			[xitem setObject:[e stringValue] forKey:[e name]];
		}
		
		
		[xitems addObject:xitem];
	}
	return xitems;
}


+ (NSDictionary *)parseSimpleXml:(NSData *)xmlData{
	DDXMLDocument *doc = [[[DDXMLDocument alloc] initWithData:xmlData options:0 error:nil]autorelease];
	//NSLog(@"%@",[doc ])
	return [self parseSimpleDDXMLElement:[doc rootElement]];
}

+ (NSMutableArray *)parseSimpleXml:(NSData *)xmlData withXpath:(NSString *)xpath{
	DDXMLDocument *doc = [[[DDXMLDocument alloc] initWithData:xmlData options:0 error:nil]autorelease];
	NSArray *items = [doc nodesForXPath:xpath error:nil];
	return [self parseSimpleDDXMLElementArray:items];
}




@end
